#!/bin/bash
commandSelf=$(readlink "$0");
if [ -z "${commandSelf}" ] ; then commandSelf="$0"; fi
commandDir=$(dirname "${commandSelf}");
toolsRoot=$(cd "${commandDir}"; pwd;);
if [ "${toolsRoot}" != "/" ] ; then toolsRoot="${toolsRoot}/"; fi
projectRoot=$(cd "${toolsRoot}../../../"; pwd;);
if [ "${projectRoot}" != "/" ] ; then projectRoot="${projectRoot}/"; fi

windowName=$(cat "${projectRoot}Srv/Conf/Conf.local.php" |grep 'PRO.serverName'|awk '{print $3}')
pidNum=$(cat "${projectRoot}Data/Logs/srv.pid")
if [ -n "${pidNum}" ] ; then sudo kill -9 "${pidNum}"; fi

# 结束窗口
tmux kill-window -t "${windowName}" >> /dev/null
sleep 1

# 初始化
tmux new -s "${windowName}" -d >> /dev/null
tmux split-window -t "${windowName}" >> /dev/null
tmux select-pane -t "${windowName}.0" >> /dev/null
tmux split-window -t "${windowName}" >> /dev/null
tmux send -t "${windowName}.0" "cd ${projectRoot}Srv/Scripts/Tools/" Enter
tmux send -t "${windowName}.0" "${projectRoot}Srv/Conf/ProtoSrc/genProto.sh && php genDocs.php && php genPostman.php && php genLang.php"
tmux send -t "${windowName}.1" "cd ${projectRoot}Data/Logs/" Enter
tmux send -t "${windowName}.1" "tail -f ${projectRoot}Data/Logs/*.log"
tmux send -t "${windowName}.2" "cd ${projectRoot}Srv/Scripts/" Enter
tmux send -t "${windowName}.2" "sudo bash" Enter
tmux send -t "${windowName}.2" "php start.local.php"
# 显示窗口
tmux attach -t ${windowName}
